package com.welsy.usermgt.command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.model.ModelLocator;
	import com.adobe.cairngorm.control.CairngormEvent;
	import flash.events.Event;
	import com.welsy.usermgt.business.SearchDelegate;
	import com.welsy.usermgt.event.SearchEvent;
	import com.welsy.usermgt.model.UserMgtModelLocator;
	import mx.controls.Alert;
	import com.welsy.usermgt.vo.SearchVO;
	import com.welsy.usermgt.vo.UsrResultVO;
	import mx.collections.ArrayCollection;
	import com.welsy.usermgt.vo.UserVO;
	import mx.rpc.IResponder;
	import com.welsy.usermgt.event.UpdateClassEvent;
	import com.welsy.usermgt.business.UpdateClassDelegate;
	import com.welsy.usermgt.vo.ClassVO;
	import mx.rpc.events.FaultEvent;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.classroom.model.ClassModelLocator;
	
	public class UpdateClassCommand implements Command, IResponder
	{
		private var model : UserMgtModelLocator = UserMgtModelLocator.getInstance();
		
		public function execute(event : CairngormEvent):void {
			var delegate : UpdateClassDelegate = new UpdateClassDelegate(this);
			var updateClassEvent : UpdateClassEvent = UpdateClassEvent(event);
			delegate.updateClassroom(updateClassEvent.classVO);
		}
		
		public function result( event : Object) : void {
			var response : XMLList = XMLList(event.result);
			//trace(response);
			//Let's parse it
			var NS : Namespace = new Namespace(NamespaceLocator.USER_MGT_DATATYPES);
			
			var TAG_CLASSROOM : QName = new QName(NS, "Classroom");
			var classroom : XMLList = response.descendants(TAG_CLASSROOM);
			
			var TAG_CLASSROOMID : QName = new QName(NS, "ClassroomId");
			var TAG_TITLE : QName = new QName(NS, "Title");
			var TAG_DESCRIPTION : QName = new QName(NS, "Description");
			var TAG_FROMDATE : QName = new QName(NS, "FromDate");
			var TAG_TODATE : QName = new QName(NS, "ToDate")
			var TAG_AUTHORID : QName = new QName(NS, "AuthorId");
			var TAG_CREATIONDATE : QName = new QName(NS, "CreationDate");
			var TAG_NOTE : QName = new QName(NS, "Note");
			
			//Update the model
			
			//Retrieve the classroom
			var classR : ClassVO = new ClassVO;
			classR.classId = int(classroom.child(TAG_CLASSROOMID));
			trace("UpdateClassroomCommand: "+classR.classId);
			classR.className = classroom.child(TAG_TITLE);
			classR.classDescr = classroom.child(TAG_DESCRIPTION);
			classR.durationFrom = ClassVO.stringToDate(classroom.child(TAG_FROMDATE));
			classR.durationTo = ClassVO.stringToDate(classroom.child(TAG_TODATE));
			classR.authorId = classroom.child(TAG_AUTHORID);
			classR.creationDate = ClassVO.stringToDate(classroom.child(TAG_CREATIONDATE));
			classR.classNotes = classroom.child(TAG_NOTE);
				
			var TAG_USERSCOLLECTION : QName = new QName(NS, "UsersCollection");
			var TAG_USERTYPE : QName = new QName(NS, "UserType");
			//Retrieve the classroom members
			var members : ArrayCollection = new ArrayCollection;
			var TAG_USERID : QName = new QName(NS, "UserId");
			var TAG_SUBSYSTEMID : QName = new QName(NS, "SubSystemId");
			var TAG_FIRSTNAME : QName = new QName(NS, "FirstName");
			var TAG_LASTNAME : QName = new QName(NS, "LastName");
			var TAG_LOGIN : QName = new QName(NS, "Login");
			var TAG_EMAIL : QName = new QName(NS, "Email");
			var TAG_ROLEID : QName = new QName(NS, "RoleId");
			classR.classUsers = new ArrayCollection;
			for each(var user : XML in response.descendants(TAG_USERTYPE)) {
				var buff : UserVO = new UserVO;
				buff.UserId = user.child(TAG_USERID);
				buff.FirstName = user.child(TAG_FIRSTNAME);
				buff.LastName = user.child(TAG_LASTNAME);
				buff.SubSystemId = user.child(TAG_SUBSYSTEMID);
				buff.Login = user.child(TAG_LOGIN);
				buff.Email = user.child(TAG_EMAIL);
				buff.RoleId = user.child(TAG_ROLEID);
				classR.classUsers.addItem(buff);
			}
			
			UserMgtModelLocator.getInstance().currentClass = classR;
			
			//asa class has been changed, refresh the classroom list
			ClassModelLocator.getInstance().classes = null;
				
		}
		
		public function fault( event : Object ) : void {
			//TODO: report the error
			Alert.show("usermgt.command.UpdateClassCommand.fault!");
			var x : FaultEvent = FaultEvent(event);
			trace(x.fault);
		}
	}
}